package clean_handler

import (
	"file-cleaner/internal/logger"
	"file-cleaner/internal/pkg/file_cleaner/cleaner"
	"file-cleaner/internal/pkg/file_cleaner/file_info"
	"os"
	"strings"
)

type BackupHandlerImpl struct {
}

func (b BackupHandlerImpl) doHandle(info *cleaner.CleanTaskInfo) bool {

	if info.SourceFileInfo().GetTag() != file_info.OnlyDelete && strings.EqualFold(info.SourceFileInfo().Path(), info.TargetFileInfo().Path()) {
		logger.Infof("Skip backup target file: %v", info.TargetFileInfo().Path())
		return true
	}

	err := os.Link(info.SourceFileInfo().Path(), info.SourceFileInfo().Path()+"."+backupSuffix)
	if err != nil {
		logger.Error(err)
		return false
	}
	logger.Infof("File: %v backed up as %v", info.SourceFileInfo().Path(), info.SourceFileInfo().Path()+"."+backupSuffix)
	return true
}
